package com.example.recyclerviewdemo.layoutmanager

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.LinearLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.recyclerviewdemo.R
import com.example.recyclerviewdemo.diffdemo.DiffUtilAdapter
import com.example.recyclerviewdemo.model.bean.ChatBean
import com.example.recyclerviewdemo.utils.DataUtil
import com.google.android.material.button.MaterialButton

class LayoutManagerActivity : AppCompatActivity() {

    private val recyclerView: RecyclerView by lazy { findViewById(R.id.recyclerView) }
    private val linear: MaterialButton by lazy { findViewById(R.id.linear) }
    private val grid: MaterialButton by lazy { findViewById(R.id.grid) }
    private val staggered: MaterialButton by lazy { findViewById(R.id.staggered) }


    //layoutManager
    private val linearLayoutManager = LinearLayoutManager(this)
    private val mLinearAdapter by lazy { DiffUtilAdapter(this) }

    private val gridLayoutManager = GridLayoutManager(this, 3).apply {

        spanSizeLookup = object :GridLayoutManager.SpanSizeLookup(){
            override fun getSpanSize(position: Int): Int {
              /*  if (mBlockItems.get(position).isCategory) {
                    return 2;
                } else {
                    return 1;
                }*/
                return 3
            }
        }

    }


    private val staggeredGridLayoutManager = StaggeredGridLayoutManager(2, LinearLayout.VERTICAL)
    private val mStaggeredAdapter by lazy { StaggeredLayoutAdapter(this) }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_layout_manager)
        initRv()
        initLayoutManager()


    }

    private fun initLayoutManager() {
        linear.setOnClickListener {
            recyclerView.layoutManager = linearLayoutManager
            recyclerView.adapter = mLinearAdapter
        }
        grid.setOnClickListener {

            recyclerView.layoutManager = gridLayoutManager
            recyclerView.adapter = mLinearAdapter
        }
        staggered.setOnClickListener {
            recyclerView.layoutManager = staggeredGridLayoutManager
            recyclerView.adapter = mStaggeredAdapter
        }

    }

    private fun initRv(){

        recyclerView.layoutManager = linearLayoutManager
        recyclerView.adapter = mLinearAdapter

        getData()
    }

    private fun getData(){
        val mutableListOf = mutableListOf<ChatBean>()
        for (i in 0..50) {
            mutableListOf.add(ChatBean(i,DataUtil.getDrawable(i), "聊天对象title$i", "聊天内容$i", 0L))
        }
        mLinearAdapter.setNewList(mutableListOf)
        mStaggeredAdapter.setNewList(mutableListOf)
    }
}